package com.zy.dao;

import com.zy.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FriendMapper
{

    /**
     * 通过好友名称和用户id查看是否已经添加
     * @param friendName 朋友名字
     * @param userId 用户id
     * @return
     */
    @Select("SELECT COUNT(*) FROM videoweb_friends AS f INNER JOIN videoweb_user AS u ON f.friend_id = u.id " +
            "WHERE u.username = #{friendName} AND f.user_id = #{userId} AND `status` = #{status}")
    public int findFriend(@Param("friendName") String friendName,@Param("userId") int userId,@Param("status") int status);

    /**
     * 通过好友名称和用户id添加申请
     * @param friendName 好友名称
     * @return
     */
    @Select("SELECT id FROM videoweb_user WHERE username = #{friendName}")
    public int findFriendByFriendName(@Param("friendName") String friendName);

    /**
     * 添加一条申请记录,状态为0
     * @param userId 用户id
     * @param friendId 好友id
     * @param status 状态
     */
    @Insert("INSERT INTO videoweb_friends (user_id,friend_id,`status`) VALUES(#{userId},#{friendId},#{status})")
    public void addFriend(@Param("userId") int userId,@Param("friendId") int friendId,@Param("status") int status);

    /**
     * 通过用户id查询好友列表
     * @param userId 用户id
     * @param friendName 好友名称
     * @return
     */
    @Select("SELECT * FROM videoweb_user AS `u` INNER JOIN videoweb_friends AS `f` ON u. id = `f`.friend_id " +
            "WHERE `f`.user_id = #{userId} AND `u`.username LIKE CONCAT('%',#{friendName},'%') AND STATUS = #{status}")
    public List<User> findFriendListByUserId(@Param("userId") int userId,@Param("friendName") String friendName,@Param("status")int status);

    /**
     * 通过用户id查询好友列表
     * @param userId 用户id
     * @return
     */
    @Select("SELECT * FROM videoweb_user AS `u` INNER JOIN videoweb_friends AS `f` ON u. id = `f`.friend_id " +
            "WHERE `f`.user_id = #{userId} AND STATUS = #{status}")
    public List<User> findFriendListByUserIdNoFriendName(@Param("userId") int userId,@Param("status")int status);

    /**
     *
     * @param userId
     * @param friendId
     * @param status
     */
    @Update("UPDATE videoweb_friends SET `status` = #{status} WHERE user_id = #{userId} AND friend_id = #{friendId}")
    public void updateFriendStatus(@Param("userId") int userId,@Param("friendId") int friendId,@Param("status") int status);

    /**
     * 删除好友申请
     * @param userId
     * @param friendId
     */
    @Delete("DELETE FROM videoweb_friends WHERE user_id = #{userId} AND friend_id = #{friendId}")
    public void deleteFriendApply(@Param("userId") int userId,@Param("friendId") int friendId);

}
